package com.yjd.back.creditline;

import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.bank.service.IBankService;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.credit.model.CreditLineApplyModel;
import com.yjd.comm.credit.service.ICreditLineApplyService;
import com.yjd.comm.enums.CreditIndustryLevel;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.dto.ContactInfo;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.dto.WorkInfo;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.user.service.IUserService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.zhima.model.ZhimaCreditDataModel;
import com.yjd.comm.zhima.service.IZhimaCreditService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 后台额度信息处理
 * Created by yangchangyan on 2017/7/12.
 */
@Controller
@Scope("prototype")
@RequestMapping("/credit")
public class CreditLineControl extends BaseControl {

    @Resource
    ICreditLineApplyService creditLineApplyService;

    @Resource
    private IUserDataService userDataService;

    @Resource
    private IUserService userService;

    @Resource
    private IBankService bankService;

    @Resource
    private IOrderService orderService;

    @Resource
    private IZhimaCreditService zhimaCreditService;

    /**
     * 额度申请信息
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "apply_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.creditLineApplyService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/credit/list"));

        List<CreditLineApplyModel> creditLineApplyModelList = (List<CreditLineApplyModel>) pager.getPagerModel().getDataList();
        List<Long> userIdList = FrameUtil.getBeanFieldValues(creditLineApplyModelList, "user_id");
        Map<Long, UserDataModel> userDataModelMap = null;
        if (creditLineApplyModelList != null && creditLineApplyModelList.size() > 0) {
            userDataModelMap = userDataService.getModelMapByIds(userIdList, DbWREnums.READ);
        }

        for (CreditLineApplyModel model : creditLineApplyModelList){
            UserDataModel userDataModel = userDataModelMap.get(model.getUser_id());
            if(null == userDataModel){
                continue;
            }
            model.getStrMap().put("id_name",userDataModel.getId_name());
            model.getStrMap().put("credit_line",userDataModel.getCredit_line());
            model.getStrMap().put("residual_amount",userDataModel.getResidual_amount());
        }
        request.setAttribute("pager", pager);
        return "credit.line.list";
    }

    /**
     * 额度审批信息
     *
     * @return void
     * @throws
     */
    @RequestMapping("/info/{id}")
    public String info(@PathVariable("id") Long id) throws Exception {
        if (id == null) {
            FrameUtil.throwBaseException1("参数错误");
        }

        CreditLineApplyModel creditLineApplyModel = creditLineApplyService.getModelById(id, DbWREnums.READ);
        if (creditLineApplyModel == null) {
            FrameUtil.throwBaseException1("当前记录不存在");
        }

        Long user_id = creditLineApplyModel.getUser_id();
        UserSession userSession = userService.getUserSession(user_id);
        if (userSession == null) {
            FrameUtil.throwBaseException1("该用户不存在");
        }

        BankModel bankModel = bankService.getModelById(user_id, DbWREnums.READ);
        if (bankModel != null) {
            userSession.getStrMap().put("bank_name", bankModel.getBank_name());
            userSession.getStrMap().put("bank_no", bankModel.getBank_card_number());
            userSession.getStrMap().put("idName", bankModel.getId_name());
            userSession.getStrMap().put("idCard", bankModel.getId_number());
            userSession.getStrMap().put("bank_mobile", bankModel.getBank_mobile());
            userSession.getStrMap().put("bank_time", bankModel.getCreate_time());
        }

        UserDataModel userDataModel = userDataService.getModelById(user_id, DbWREnums.READ);
        if (userDataModel != null) {
            userSession.getStrMap().put("residual_amount", userDataModel.getResidual_amount());
            if (userDataModel.getJob_info() != null) {
                WorkInfo workInfo = StringUtil.getObject(userDataModel.getJob_info(), WorkInfo.class);
                if (workInfo != null) {
                    userSession.getStrMap().put("company", workInfo.getCompany());
                    userSession.getStrMap().put("companyType", workInfo.getCompanyType());
                    userSession.getStrMap().put("companyLocation", workInfo.getCompanyLocation());
                    userSession.getStrMap().put("residenceLocation", workInfo.getResidenceLocation());
                    userSession.getStrMap().put("duration", workInfo.getDuration());
                }
            }

            if (userDataModel.getContacts() != null) {
                ContactInfo contactInfo = StringUtil.getObject(userDataModel.getContacts(), ContactInfo.class);
                if (contactInfo != null) {
                    userSession.getStrMap().put("contacName", contactInfo.getIdName());
                    userSession.getStrMap().put("contacRelation", contactInfo.getRelation());
                    userSession.getStrMap().put("contacMoblie", contactInfo.getMobile_number());
                }
            }
        }

        // 用户芝麻信息
        ZhimaCreditDataModel zhimaInfo = zhimaCreditService.zhimaData(userSession.getUser_id());
        zhimaInfo.getStrMap().put("creditLabel", CreditIndustryLevel.parse(zhimaInfo.getIndustry_level()).getLabel());
        request.setAttribute("zhimaInfo", zhimaInfo);

        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("user_id",userSession.getUser_id());
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(orderService.getOrderInfoList(paramMap), paramMap, ServletUtil.getUrl("/credit/info/"+id));
        for(OrderModel orderModel :(List<OrderModel>)pager.getPagerModel().getDataList()){
            orderModel.getStrMap().put("status", StagesStatusEnum.parse(orderModel.getStatus()).getLabel());
        }
        request.setAttribute("pager", pager);
        request.setAttribute("user", userSession);
        request.setAttribute("credit", creditLineApplyModel);
        return "credit.line.info";
    }

    /**
     * 额度审批
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/apply")
    public void apply(Long id, Integer status) throws Exception {
        creditLineApplyService.apply(id, status);
        super.successMsg(ServletUtil.getUrl("/credit/list"), "审核成功");
    }
}
